<script lang="ts" setup>

import {CirclePlus,StarFilled,Star,Delete} from '@element-plus/icons-vue'

import {ref} from 'vue'
import type {TaskListType} from '../../../type/EditType'
import {getCurrentDate} from '@/utils/UtilFn'
import { BroadCaster } from '@/utils/BroadCaster';
import { ElMessage } from 'element-plus';

// 任务队列
let taskList = ref<TaskListType[]>([
  {
    id:1,
    name:"新建任务",
    isCollect:false,
    desc:"这是大型测试任务",
    time:"12:00:10"
  },{
    id:2,
    name:"新建任务12",
    isCollect:false,
    desc:"这是大型测试任务22",
    time:"12:20:10"
  }
]);

let AddTaskVisible = ref(false)
let addFromData = ref({
  name:"",
  desc:''
})
// 新增任务队列
const addTask = ()=>{
  AddTaskVisible.value = true;

}
// 新增任务提交
const onAddSubmit = () =>{
  let id = parseInt((Math.random()* 1000).toString());
  taskList.value.push({
    id:id,
    name:addFromData.value.name,
    isCollect:false,
    desc:addFromData.value.desc,
    time:getCurrentDate()
  })
  selectCurrentHighligh.value = id;
  onAddClose()
}
// 新增任务取消
const onAddClose = ()=>{
  AddTaskVisible.value = false;
}

// 修改收藏状态
const ChangeIsCollect = (item:TaskListType)=>{
  item.isCollect = !item.isCollect
}
// 删除条任务
const removeTaskItem = (target:TaskListType)=>{
  
  taskList.value.forEach((item:TaskListType,index:number)=>{
    if(target.id == item.id){
      taskList.value.splice(index,1)
      return true;
    }
  })
}

let BroadCasterObj = new BroadCaster()

// 要高亮的一项
let selectCurrentHighligh = ref<string|number>("")
// 更换选中项，并高亮
const onChangeHighLight = (item:TaskListType)=>{
  selectCurrentHighligh.value = item.id

  BroadCasterObj.RemindFn("onChangeHighLight",item)
}

// 监听数据并保存
const onChangeSaveDrawData = (item:any)=>{
  if(!selectCurrentHighligh.value){
    ElMessage({
      message: '请选择任务',
      type: 'warning',
    })
    return;
  }
  console.log(item)
}
BroadCasterObj.addFn("onChangeSaveDrawData",onChangeSaveDrawData)
</script>
<template>
  <div class="drawPanel">
    <div class="drawPanelBox">
      <div class="PanelTitle">
        任务列表
        <div class="operateBut">
          <el-icon @click="addTask"><CirclePlus /></el-icon>
        </div>
      </div>
      <div class="PanelContent">
        <el-scrollbar height="calc(100% - 27px)">
            <ul class="panelList">
              <li :class="[selectCurrentHighligh==item.id?'selectHighlight':'']" @click="onChangeHighLight(item)" v-for="(item,index) in taskList" :key="index">
                <div class="taskTitle">
                  <b>{{ item.name }}</b>
                  <div>
                    <el-icon @click="ChangeIsCollect(item)" v-if="item.isCollect"><StarFilled /></el-icon>
                    <el-icon @click="ChangeIsCollect(item)" v-else><Star /></el-icon>
                    <el-icon @click="removeTaskItem(item)"><Delete /></el-icon>
                  </div>
                </div>
                <div class="tasklist_ps">
                  <span>简介：{{ item.desc }}</span>
                  <time>{{ item.time }}</time>
                </div>
              </li>
            </ul>
        </el-scrollbar>
      </div>
    </div>
    <el-dialog
      v-model="AddTaskVisible"
      title="新增任务"
      width="500">
      <div class="addTaskDialog">
        <el-form :model="addFromData" label-width="auto" style="max-width: 500px">
          <el-form-item label="任务名称">
            <el-input v-model="addFromData.name" />
          </el-form-item>
          <el-form-item label="任务简介">
            <el-input v-model="addFromData.desc" type="textarea" />
          </el-form-item>
          <el-form-item>
            <div class="addTaskBut">
              <el-button type="primary" @click="onAddSubmit">提交</el-button>
              <el-button @click="onAddClose">取消</el-button> 
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<style scoped>
  .drawPanel{
    position: absolute;
    z-index: 100;
    width: 400px;
    height: 48%;
    background-color: #468adc75;
    top: 10%;
    left: 1%;
    border-radius: 8px;
  }
  .drawPanelBox{
    width: 100%;
    height: 100%;
  }
  .PanelTitle{
    width: 100%;
    height: 40px;
    position: relative;
    color: #fff;
    text-align: center;
    line-height: 40px;
    font-size: 21px;
    margin: 3px 0 10px 0;
  }
  .operateBut{
    position: absolute;
    top: 3px;
    right: 10%;
  }
  .operateBut i{
    margin: 0 5px;
    cursor: pointer;
    font-size: 20px;
  }
  .PanelContent{
    height: calc(100% - 40px);
    width: 100%;
  }
  .panelList{
    width: 86%;
    margin: auto;
    list-style-type: none;
    color: #fff;
  }
  .panelList li{
    width: 100%;
    margin: 5px 0 0 0;
    cursor: pointer;
    border-bottom: 1px solid;
    border-image: radial-gradient(#fafbff, #fcfcfc00) 5;
    padding-bottom: 5px;
  }
  .panelList li:hover{
    background-color: rgba(255,255,255,0.19);
    transition: all .5s;
    border-radius: 10px;
  }
  .taskTitle{
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin: 0 0 6px 0;
  }
  .taskTitle b{
    font-size: 17px;
    font-weight: normal;
  }
  .taskTitle i{
    margin-right: 10px;
    cursor: pointer;
    font-size: 18px;
    line-height: 25px;
    margin-top: 2px;
  }
  .tasklist_ps{
    width: 100%;
    display: flex;
    justify-content: space-between;
    font-size: 13px;
    color: #dfdfdf;
  }
  .tasklist_ps span{
    display: block;
    width: 80%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .addTaskBut{
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
  .selectHighlight{
    background: url(../../../assets/logo.svg);
  }
</style>
